<?php
class User extends CI_Controller{

    public function __construct($value=''){
        parent::__construct();
        $this->load->helpers(array('form','url'));
    }

    public function index(){
       $this->load->helpers('url');
       $this->load->view('signin.html');
        }

    public function checx(){

        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('signin.html');
        }
        else
        {
            $this->load->view('success');
        }
    }

    public function display(){
        $this->load->view('reg.html');
    }

    //用户注册
    public function reg(){
        $this->load->database();
        $this->output->enable_profiler(TRUE);
        //检查用户输入的数据
        $this->load->library('form_validation');

        $this->load->form_validation->set_rules('email','email','  
                                    trim|required|valid_email|is_unique[users,uemail]');
        $this->form_validation->set_rules('password0','密码','required|min_length[6]');
        $this->form_validation->set_rules('password1', '重复密码', 'callback_password_check');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('reg.html');
        }
        else
        {
            $this->load->view('success');
        }

        //存放到数据库
        $this->load->model('UserModel');
            $data = array(
                'email' => $_POST['email'],
                'password' => md5($_POST['password0'])
                );
            $this->UserModel->add($data);
        //跳转到登录界面
            $this->load->view('signin.html');

    }
    public function password_check()
    {
        if ($_POST['password0'] !== $_POST['password1'])
        {
            $this->form_validation->set_message('password_check', '两次密码不一致！');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

}